export const getStorage = theKey => new Promise((resolve, reject) => {
  wx.getStorage({
    key: theKey,
    success(res) {
      resolve(res.data)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const setStorage = (theKey, theValue) => new Promise((resolve, reject) => {
  wx.setStorage({
    key: theKey,
    data: theValue,
    success() {
      resolve()
    },
    fail(e) {
      reject(e)
    }
  })
})
export const getLocation = (obj) => new Promise((resolve) => {
  wx.getLocation({
    ...obj,
    success({
      latitude,
      longitude
    }) {
      resolve({
        latitude,
        longitude
      })
    },
    fail() {
      resolve({
        latitude: -999,
        longitude: -999
      })
    }
  })
})
export const chooseLocation = () => new Promise((resolve, reject) => {
  wx.chooseLocation({
    success({
      address,
      latitude,
      longitude,
      name
    }) {
      resolve({
        address,
        latitude,
        longitude,
        name
      })
    },
    fail(e) {
      reject(e)
    }
  })
})
export const removeStorage = theKey => new Promise((resolve, reject) => {
  wx.removeStorage({
    key: theKey,
    success(res) {
      resolve(res.data)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const clearStorage = () => new Promise((resolve, reject) => {
  try {
    wx.clearStorageSync()
    resolve()
  } catch (e) {
    reject(e)
  }
})
export const login = () => new Promise((resolve, reject) => {
  wx.login({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const request = obj => new Promise((resolve, reject) => {
  wx.request({
    url: obj.url,
    data: obj.data,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      ...obj.header
    },
    method: obj.method,
    success(res) {
      resolve(res.data.data)
    },
    fail(e) {
      console.log(e)
      reject(e)
    }
  })
})
export const post = (url, data) => new Promise((resolve, reject) => {
  wx.request({
    url: url,
    data: data,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    success(res) {
      resolve(res.data.data)
    },
    fail(e) {
      console.log(e)
      reject(e)
    }
  })
})
export const chooseImage = (count = 1, sourceType = ['album', 'camera']) => new Promise((resolve, reject) => {
  wx.chooseImage({
    count,
    sizeType: 'compressed',
    sourceType,
    success(res) {
      resolve(res)
    },
    fail(e) {
      console.log(e)
      reject(e)
    }
  })
})
export const getImageInfo = (url) => new Promise((resolve, reject) => {
  wx.getImageInfo({
    src: url,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const downloadFile = (url) => new Promise((resolve, reject) => {
  wx.downloadFile({
    url, // 仅为示例，并非真实的资源
    success(res) {
      // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
      if (res.statusCode === 200) {
        resolve(res.tempFilePath)
      }
    },
    fail(err) {
      reject(err)
    }
  })
})
export const showShareMenu = () => {
  return wx.showShareMenu
}
export const pageScrollTo = () => {
  return wx.pageScrollTo
}
export const getUserInfo = (obj) => new Promise((resolve, reject) => {
  wx.getUserInfo({
    ...obj,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const alert = (content) => {
  wx.showModal({
    title: '',
    content,
    showCancel: false
  })
}
export const showModal = (title, content, confirmColor = '#3cc51f') => new Promise((resolve, reject) => {
  wx.showModal({
    title,
    content,
    confirmColor,
    cancelColor: '#999999',
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const navigateTo = (url) => new Promise((resolve, reject) => {
  wx.navigateTo({
    url: url,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const redirectTo = (redirectUrl) => new Promise((resolve, reject) => {
  wx.redirectTo({
    url: redirectUrl,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const startPullDownRefresh = () => new Promise((resolve, reject) => {
  wx.startPullDownRefresh({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const stopPullDownRefresh = () => new Promise((resolve, reject) => {
  wx.stopPullDownRefresh({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const uploadFile = (obj) => new Promise((resolve, reject) => {
  wx.uploadFile({
    ...obj,
    name: 'file',
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})
export const requestPayment = (payInfo) => new Promise((resolve, reject) => {
  wx.requestPayment({
    'timeStamp': payInfo.timeStamp,
    'nonceStr': payInfo.nonceStr,
    'package': payInfo.package,
    'signType': 'MD5',
    'paySign': payInfo.paySign,
    'success': function (res) {
      wx.showToast({
        title: '支付成功',
        icon: 'success',
        mask: true,
        duration: 1500
      })
      setTimeout(() => {
        resolve(true)
      }, 1500)
    },
    'fail': function (err) {
      // 支付失败
      wx.showToast({
        title: '支付失败',
        icon: 'none',
        mask: true,
        duration: 1500
      })
      setTimeout(() => {
        reject(err)
      }, 1500)
    }

  })
})
export const setNavigationBarTitle = (title) => new Promise((resolve, reject) => {
  wx.setNavigationBarTitle({
    title,
    success(res) {
      resolve(res)
    },
    fail(err) {
      reject(err)
    }
  })
})
export const openDocument = (fileUrl) => new Promise((resolve, reject) => {
  wx.downloadFile({
    // 示例 url，并非真实存在
    url: fileUrl,
    success: function (res) {
      const filePath = res.tempFilePath
      wx.openDocument({
        filePath: filePath,
        success: function (res) {
          resolve(res)
        }
      })
    }
  })
})
export const showActionSheet = (itemList) => new Promise((resolve, reject) => {
  wx.showActionSheet({
    itemList,
    // itemColor: '',
    success: function (res) {
      resolve(res.tapIndex)
    },
    fail: function (res) {}
  })
})

export const previewImage = (urls, i = 0) => new Promise((resolve, reject) => {
  wx.previewImage({
    urls: urls, // 需要预览的图片http链接列表,
    current: urls[i],
    success(res) {
      resolve(res)
    },
    fail(err) {
      reject(err)
    }
  })
})
export const showLoading = (title) => new Promise((resolve, reject) => {
  wx.showLoading({
    title,
    mask: true,
    success(res) {
      resolve(res)
    },
    fail(err) {
      reject(err)
    }
  })
})

export const makePhoneCall = (number) => new Promise((resolve, reject) => {
  wx.makePhoneCall({
    phoneNumber: number
  })
})
export default {
  makePhoneCall,
  showLoading,
  previewImage,
  showActionSheet,
  showModal,
  openDocument,
  requestPayment,
  getStorage,
  setStorage,
  getLocation,
  chooseLocation,
  removeStorage,
  clearStorage,
  login,
  request,
  chooseImage,
  getImageInfo,
  downloadFile,
  showShareMenu,
  pageScrollTo,
  getUserInfo,
  alert,
  navigateTo,
  redirectTo,
  startPullDownRefresh,
  stopPullDownRefresh,
  uploadFile
}
